//
//  CalendarDayCell.m
//  tttttt
//
//  Created by 张凡 on 14-8-20.
//  Copyright (c) 2014年 张凡. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "CalendarDayCell.h"
#import "CaBackView.h"
@implementation CalendarDayCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    
    //边框
    CaBackView* v = [[CaBackView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    v.backgroundColor = [UIColor whiteColor];
    [self addSubview:v];

    //选中时显示的图片
    imgview = [[UIImageView alloc]initWithFrame:CGRectMake(0 , 0, 25, 25)];
    imgview.image = [UIImage imageNamed:@"todayC_n"];
    [self addSubview:imgview];
    
    //日期
    day_lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 2, 25, 18)];
    day_lab.textAlignment = NSTextAlignmentCenter;
    day_lab.font = [UIFont systemFontOfSize:20];
    [self addSubview:day_lab];
    
    //农历
    day_title = [[UILabel alloc]initWithFrame:CGRectMake(2, 21, 33,15)];
    day_title.textColor = [UIColor lightGrayColor];
    day_title.font = [UIFont boldSystemFontOfSize:10];
    day_title.textAlignment = NSTextAlignmentLeft;
    [self addSubview:day_title];
}


- (void)setModel:(CalendarDayModel *)model
{
    [numBtn removeFromSuperview];
    [view1 removeFromSuperview];
    [view2 removeFromSuperview];
    if (model.style != CellDayTypeEmpty) {
        if ([(UnreadModel*)[_unreadModelArray firstObject] eventCount]!=0) {
            numBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            numBtn.frame = CGRectMake(self.frame.size.width - 17, 2, 14, 14);
            numBtn.layer.cornerRadius = 7;
            numBtn.backgroundColor = [UIColor redColor];
            [numBtn setTitle: [NSString stringWithFormat:@"%d",[[_unreadModelArray firstObject] eventCount]] forState:UIControlStateNormal];
            numBtn.titleLabel.font = [UIFont systemFontOfSize:12];
            numBtn.userInteractionEnabled = NO;
            [self addSubview:numBtn];
        }
        
        if ([(MSEvent*)[_eventModelArray firstObject] eventTitle]) {
            view1 = [[CalendarEventView alloc]initWithFrame:CGRectMake(2, 40, self.frame.size.width - 5, 13)];
            view1.backgroundColor = [UIColor whiteColor];
            view1.title = [(MSEvent*)[_eventModelArray firstObject] eventTitle];
            [self addSubview:view1];
            
            if (_eventModelArray.count>1)
            {
                view2 = [[CalendarEventView alloc]initWithFrame:CGRectMake(2, 55, self.frame.size.width - 5, 13)];
                view2.backgroundColor = [UIColor whiteColor];
                view2.title = [(MSEvent*)_eventModelArray[1] eventTitle];
                [self addSubview:view2];
            }
        }

    }
    
    switch (model.style) {
        case CellDayTypeEmpty://不显示
            [self hidden_YES];
            break;
            
        case CellDayTypePast://过去的日期
            [self hidden_NO];
            
            if (model.holiday) {
                day_lab.text = model.holiday;
            }else{
                day_lab.text = [NSString stringWithFormat:@"%lu",(unsigned long)model.day];
            }
            
            day_lab.textColor = [UIColor lightGrayColor];
            day_title.text = model.Chinese_calendar;
            imgview.hidden = YES;
            break;
            
        case CellDayTypeFutur://将来的日期
            [self hidden_NO];
            
            if (model.holiday) {
                day_lab.text = model.holiday;
                day_lab.textColor = [UIColor orangeColor];
            }else{
                day_lab.text = [NSString stringWithFormat:@"%lu",(unsigned long)model.day];
                day_lab.textColor = [UIColor blackColor];
            }
            
            day_title.text = model.Chinese_calendar;
            imgview.hidden = YES;
            break;
            
        case CellDayTypeWeek://周末
            [self hidden_NO];
            
            if (model.holiday) {
                day_lab.text = model.holiday;
                day_lab.textColor = [UIColor orangeColor];
            }else{
                day_lab.text = [NSString stringWithFormat:@"%lu",(unsigned long)model.day];
                day_lab.textColor = [UIColor lightGrayColor];
            }
            
            day_title.text = model.Chinese_calendar;
            imgview.hidden = YES;
            break;
            
        case CellDayTypeClick://被点击的日期
            [self hidden_NO];
            day_lab.text = [NSString stringWithFormat:@"%lu",(unsigned long)model.day];
            day_lab.textColor = [UIColor blackColor];
            day_title.text = model.Chinese_calendar;
            imgview.hidden = NO;
            
            break;
            
        default:
            
            break;
    }
}

- (void)hidden_YES{
    
    day_lab.hidden = YES;
    day_title.hidden = YES;
    imgview.hidden = YES;
    
}


- (void)hidden_NO{
    
    day_lab.hidden = NO;
    day_title.hidden = NO;
    
}


@end
